﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DTO_HealthInsurance;
using BLL_HealthInsurance;
namespace HealthInsuranceMgm.Admin
{
    public partial class PolicyRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnDelete.Attributes.Add("OnClick", "return confirm('Are you Sure?')");

            }

            PanelListEmp.Visible = true;
            PanelUpdatestatus.Visible = false;
            load();
        }
        public void load()
        {
            BLLPolicyRequest bll = new BLLPolicyRequest();
            grvpore.DataSource = bll.ListPolicy();
            grvpore.DataBind();
        }

        public void LoadData(int requestId)
        {

            BLLPolicyRequest obj = new BLLPolicyRequest();
            DTOPolicyRequestDetail objDt = new DTOPolicyRequestDetail();
            objDt.RequestId = requestId;
            DataTable dt = obj.GetPolicyRequsetById(objDt);


            lblrequestId.Text = dt.Rows[0]["RequestId"].ToString();
            lblRequestDate.Text = dt.Rows[0]["RequestDate"].ToString();
            lblpolicyName.Text = dt.Rows[0]["PolicyName"].ToString();
            lblpolicyId.Text = dt.Rows[0]["PolicyId"].ToString();

            lblPolicyAmount.Text = dt.Rows[0]["PolicyAmount"].ToString();
            lblEmpNo.Text = dt.Rows[0]["EmpNo"].ToString();
            lblEmi.Text = dt.Rows[0]["Emi"].ToString();
            lblCompanyName.Text = dt.Rows[0]["CompanyName"].ToString();

            lblCompanyId.Text = dt.Rows[0]["CompanyId"].ToString();
            lblstatus.Text = dt.Rows[0]["Status"].ToString();



        }
        public SortDirection SortDirection
        {
            get
            {

                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }
        protected void grvpore_Sorting(object sender, GridViewSortEventArgs e)
        {
            BLLPolicyRequest obj = new BLLPolicyRequest();
            string sortExpression = e.SortExpression;
            string Direction = "";

            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                Direction = "DESC";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                Direction = "ASC";
            }
            DataTable dt = obj.ListPolicy();
            dt.DefaultView.Sort = sortExpression + " " + Direction;
            grvpore.DataSource = dt;
            grvpore.DataBind();
        }

        protected void grvpore_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int requestId = Int32.Parse(grvpore.DataKeys[e.NewSelectedIndex].Value.ToString());
            LoadData(requestId);
            DTOPolicyRequestDetail objdt = new DTOPolicyRequestDetail();
            objdt.RequestId = requestId;

             BLLPolicyRequest obj = new BLLPolicyRequest();
             DataTable dt = obj.GetPolicyRequsetById(objdt);
            
             string status = dt.Rows[0]["Status"].ToString();
             if (status.Equals("Approval"))
             {
                 btnupdate.Enabled = false;
             }
             else {
                 btnupdate.Enabled = true;
             }
            PanelListEmp.Visible = false;
            PanelUpdatestatus.Visible = true;

        }






        protected void grvpore_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvpore.PageIndex = e.NewPageIndex;
            load();
        }
        public void message(string script)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", script, true);
        }
        protected void btnupdate_Click(object sender, EventArgs e)
        {
            BLLPolicyApproval objbll = new BLLPolicyApproval();
            DTOPolicyApprovalDetails objdt = new DTOPolicyApprovalDetails();

            BLLPolicyRequest objre = new BLLPolicyRequest();
            DTOPolicyRequestDetail objdto = new DTOPolicyRequestDetail();

            objdt.PolicyId = Int32.Parse(lblpolicyId.Text);
            objdt.RequestId = Int32.Parse(lblrequestId.Text);
            objdt.ApprovalDate = DateTime.Now.ToString();
            objdt.ApprovalAmount = float.Parse(lblPolicyAmount.Text);
            objdt.ApprovalStatus = "Approval";
            objdt.ApprovalReason = "OK";

            objdto.CompanyId =Int32.Parse( lblCompanyId.Text);
            objdto.CompanyName = lblCompanyName.Text;
            objdto.EmpNo = Int32.Parse( lblEmpNo.Text);
            objdto.PolicyName = lblpolicyName.Text;
            objdto.PolicyEmi = float.Parse( lblEmi.Text);
            objdto.PolicyId = Int32.Parse(lblpolicyId.Text);
            objdto.RequestId = Int32.Parse(lblrequestId.Text);

            objdto.RequestDate = DateTime.Now.ToString();
            objdto.PolicyAmount = float.Parse(lblPolicyAmount.Text);

            objdto.Status = "Approval";

            objre.updatePolicyRequset(objdto);

            int i = objbll.insertPolicy(objdt);
            load();
            if (i > 0)
            {
                message("alert('Successfull')");
                load();
            }



        }

        protected void btnDelete_Click1(object sender, EventArgs e)
        {
            BLLPolicyApproval objapp = new BLLPolicyApproval();
            DTOPolicyApprovalDetails objdto = new DTOPolicyApprovalDetails();

            BLLPolicyRequest objbll = new BLLPolicyRequest();
            DTOPolicyRequestDetail objdt = new DTOPolicyRequestDetail();
            objdt.RequestId = Int32.Parse(lblrequestId.Text);
            objdto.PolicyId = Int32.Parse(lblpolicyId.Text);
            objbll.deletePolicyRequset(objdt);
            objapp.deletePolicy(objdto);
            load();


        }

    }
}